/**
 * Created by zycool on 2016/11/1.
 * 专门建立此文件来处理维修记录详情展示 - 腐蚀控制
 * 注意： 这里和管体缺陷有少许不同之处
 * 具体哪里不同，我也快忘记了。
 */


var recordContent = {
    head: '',
    START_NODE: '',
    CORROSION_ENVIRONMENT_NODE: '',
    COAT_SURVEY_NODE: '',
    SURFACE_TREATMENT_NODE: '',
    CP_SURVEY_NODE: '',
    DEFECT_MEASURE_NODE: '',
    REPAIR_NODE: '',
    COAT_REPAIR_NODE: '',
    END_NODE: '',
    CTRL_LEAKAGE_POINT_NODE: '',
    body: ''
};


/** 涂层维修 8 大节点 - start **/
// 管沟开挖 - 1
var START_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'buriedDepth', title: '埋深(m)：'},
    {
        name: 'group', title: '缺陷尺寸：', node: [
        {name: 'length', title: '长(m) '},
        {name: 'width', title: '宽(m) '},
        {name: 'depth', title: '深(m) '}
    ]
    },
    {name: 'remark', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];

// 腐蚀环境调查 - 2
var CORROSION_ENVIRONMENT_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'deepRootedPlants', title: '根系植物：'},
    {name: 'groundwater', title: '地下水：'},
    {name: 'soilColor', title: '土壤颜色：'},
    {name: 'soilCompactness', title: '土壤松紧度：'},
    {name: 'soilHumidity', title: '土壤干湿度：'},
    {name: 'soilLayer', title: '土壤分层描述：'},
    {name: 'soilParticlesGroup', title: '土壤颗粒组划分：'},
    {name: 'soilPh', title: 'PH值：'},
    {name: 'soilResistivity', title: '土壤电阻率：'},
    {name: 'soilSamplingNumber', title: '土壤取样编号：'},
    {name: 'terrainGeomorphology', title: '地形地貌描述：'},
    {name: 'remark', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];

// 漏损点测量 - 3
var CTRL_LEAKAGE_POINT_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'maxLength', title: '最大漏损点长度(mm)：'},
    {name: 'maxWidth', title: '最大漏损点宽度(mm)：'},
    {name: 'clock', title: '时钟位置：'},
    {name: 'description', title: '漏损点外观描述：'},
    {name: 'remarks', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];

// 防腐层调查 - 4
var COAT_SURVEY_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'appearance', title: '外观质量：'},
    {name: 'coatingMaterial', title: '防腐层类型：'},
    {name: 'peelingStrengthTest1', title: '剥离强度(N/cm)-测试1：'},
    {name: 'peelingStrengthTest2', title: '剥离强度(N/cm)-测试2：'},
    {name: 'peelingStrengthTest3', title: '剥离强度(N/cm)-测试3：'},
    {name: 'peelingStrengthTest4', title: '剥离强度(N/cm)-测试4：'},
    {name: 'electricSparkAmount', title: '电火花检测-漏点数(个)：'},
    {name: 'electricSparkTestVoltage', title: '电火花检测-检查电压(KV)：'},
    {name: 'coatingThicknessTest1', title: '厚度(mm)-测试1：'},
    {name: 'coatingThicknessTest2', title: '厚度(mm)-测试2：'},
    {name: 'coatingThicknessTest3', title: '厚度(mm)-测试3：'},
    {name: 'coatingThicknessTest4', title: '厚度(mm)-测试4：'},
    {name: 'pipeBodyTemperature', title: '管体温度(℃)：'},
    {name: 'remark', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];

// 表面处理 - 5
var SURFACE_TREATMENT_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'anchorDepth', title: '锚纹深度(mm)：'},
    {name: 'derustingGrade', title: '除锈等级：'},
    {name: 'remark', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];

// 阴保调查 - 6
var CP_SURVEY_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'soilResistivity', title: '土壤电阻率：'},
    {name: 'freeCorrosionPotential', title: '自然电位(mV)：'},
    {name: 'onPotential', title: '通电电位(mV)：'},
    {name: 'polarizedPotential', title: '极化电位(mV)：'},
    {name: 'maxPotential', title: '直流干扰最大值(mv)：'},
    {name: 'minPotential', title: '直流干扰最小值(mv)：'},
    {name: 'acPotential', title: '交流干扰电压(V)：'},
    {name: 'remark', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];

// 防腐层修复 - 7
var COAT_REPAIR_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'edmLeak', title: '电火花检漏(电压KV)：'},
    {name: 'environmentalTemperature', title: '环境温度(℃)：'},
    {name: 'isJoint', title: '是否为补口：'},
    {name: 'materialManufacturer', title: '修复材料厂商：'},
    {name: 'repairLength', title: '修复长度(mm)：'},
    {name: 'repairMaterial', title: '修复材料：'},
    {name: 'repairThickness', title: '修复厚度(mm)：'},
    {name: 'surfaceTemperature', title: '管表温度(℃)：'},
    {name: 'workload', title: '工作量(㎡)：'},
    {name: 'remark', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];

// 管沟回填 - 8
var END_NODE = [
    {name: 'isCompletion', title: '是否完成：'},
    {name: 'remark', title: '备注：'},
    {name: 'imgList', title: '照片：'}
];
/** 涂层维修 8 大节点 - end **/

function loopNodeProperty(data, node, type, title, number, length, id) {
    if (!data) {
        return false;
    }else{
        data = DyyBase.clearNull(data);
    }

    var head = '', body = '<p class="zy-dy-p-head">现场测量数据：</p>';

    for (var i = 0, len = node.length; i < len; i++) {
        if (node[i]['name'] === 'isCompletion') {
            var cls = 'green';
            if (data.hasOwnProperty('isCompletion') && data['isCompletion'] == 0) {
                cls = 'gray';
            }
            if (number == length - 1) {
                head += '<p><span><i class="zy-num-click ' + cls + '" data-node="' + type + '" data-id="' + id + '">' + (++number) + '</i>' + title + '</span></p>';
            } else {
                head += '<p><span><i class="zy-num-click ' + cls + '" data-node="' + type + '" data-id="' + id + '">' + (++number) + '</i>' + title + '</span></p><p class="zy-dy-bg"></p>';
            }
        } else {
            if (node[i]['name'] === 'group') {
                var temp = '';
                for (var j = 0, lens = node[i]['node'].length; j < lens; j++) {
                    if (data.hasOwnProperty(node[i]['node'][j]['name'])) {
                        temp += node[i]['node'][j]['title'] + '<span>' + data[node[i]['node'][j]['name']] + '</span>';
                    }
                }
                body += '<p class="zy-dy-p-span">' + node[i]['title'] + temp + '</p>';
            } else if (node[i]['name'] == 'imgList') {
                if (data.hasOwnProperty('imgList')) {
                    if (data['imgList']) {
                        body += '<p class="zy-dy-p-head">现场照片：</p>';
                        if (data['imgList'].hasOwnProperty('ALL')) {
                            var imgs = '';
                            for (var n = 0, lenth = data['imgList']['ALL'].length; n < lenth; n++) {
                                imgs += '<span><a target="_blank" href="' + DyyBase.file_path + data['imgList']['ALL'][n]['path'] + '"><img src="' + DyyBase.file_path + data['imgList']['ALL'][n]['path'] + '"></a></span>';
                            }
                            body += '<p class="zy-imglist">' + imgs + '</p>';
                        }
                        if (data['imgList'].hasOwnProperty('REPAIR_BEFORE')) {
                            var before = '';
                            for (var a = 0, lena = data['imgList']['REPAIR_BEFORE'].length; a < lena; a++) {
                                before += '<span><a target="_blank" href="' + DyyBase.file_path + data['imgList']['REPAIR_BEFORE'][a]['path'] + '"><img src="' + DyyBase.file_path + data['imgList']['REPAIR_BEFORE'][a]['path'] + '"></a></span>';
                            }
                            body += '<p class="zy-dy-p-head">修复前：</p><p class="zy-imglist">' + before + '</p>';
                        }
                        if (data['imgList'].hasOwnProperty('REPAIR_DURING')) {
                            var middle = '';
                            for (var b = 0, lenb = data['imgList']['REPAIR_DURING'].length; b < lenb; b++) {
                                middle += '<span><a target="_blank" href="' + DyyBase.file_path + data['imgList']['REPAIR_DURING'][b]['path'] + '"><img src="' + DyyBase.file_path + data['imgList']['REPAIR_DURING'][b]['path'] + '"></a></span>';
                            }
                            body += '<p class="zy-dy-p-head">修复中：</p><p class="zy-imglist">' + middle + '</p>';
                        }
                        if (data['imgList'].hasOwnProperty('REPAIR_AFTER')) {
                            var after = '';
                            for (var c = 0, lenc = data['imgList']['REPAIR_AFTER'].length; c < lenc; c++) {
                                after += '<span><a target="_blank" href="' + DyyBase.file_path + data['imgList']['REPAIR_AFTER'][c]['path'] + '"><img src="' + DyyBase.file_path + data['imgList']['REPAIR_AFTER'][c]['path'] + '"></a></span>';
                            }
                            body += '<p class="zy-dy-p-head">修复后：</p><p class="zy-imglist">' + after + '</p>';
                        }
                    }
                }
            } else {
                if (data.hasOwnProperty(node[i]['name'])) {
                    if (node[i]['name'] == 'remark' || node[i]['name'] == 'remarks') {
                        body += '<p class="zy-dy-p-remark">' + node[i]['title'] + '<span>' + data[node[i]['name']] + '</span></p>';
                    } else {
                        body += '<p class="zy-dy-p-span">' + node[i]['title'] + '<span>' + data[node[i]['name']] + '</span></p>';
                    }
                }
            }
        }
    }

    body = body + '<div class="clear"></div>';

    recordContent.head += head;

    if (!recordContent.body) {
        recordContent.body = body;
    }

    recordContent[type] = body;
}

function loopNodeData(data, id, code, tr, status, tag) {
    var $dy_tr = $('<tr class="zy-dy-tr" data-defectId-repair="' + id + '"><td colspan="12"><div class="zy-details-box"><div class="zy-dy-inner"></div></div></td></tr>');
    var $dy_box = $dy_tr.find('div.zy-dy-inner');
    var $repair_status = $('<div class="zy-dy-status"></div>');
    var $repair_body = $('<div class="zy-dy-info"></div>');
    var $repair_buttons = $('<div class="zy-work-repair-button"></div>');

    // 全部完成标识
    var $all_done = '<div class="zy-work-repair-done"><i class="zy-repair-done-icon"></i><p>编号 ' + code + ' 的缺陷点已维修完成。</p></div>';
    // 收起按钮
    var $pick_btn = $('<button type="button" class="zy-button zy-pick-up" data-id="' + id + '"><i></i>收起</button>');
    $pick_btn.click(function () {
        $pick_btn.parents('tr.zy-dy-tr').hide();
        tag.removeClass('active');
    });
    // 作业点维修记录
    var $work_btn = $('<button class="zy-button-s zy-delete-s r" type="button" data-id="' + id + '"><i class="zy-delete-icon"></i>作业点维修记录</button>');
    $work_btn.click(function () {
        window.location.href = DyyBase.root_path + '/corrctrl/repairMgr/toRepairWorkRecord.html?id=' + id;
    });

    /** 主体 - start **/
    for (var i = 0, len = data.length; i < len; i++) {
        switch (true) {
            case (data[i].hasOwnProperty('START_NODE')):
                if(data[i]['START_NODE']){
                    loopNodeProperty(data[i]['START_NODE'], START_NODE, 'START_NODE', '管沟开挖', i, len, id);
                }
                break;
            case (data[i].hasOwnProperty('CORROSION_ENVIRONMENT_NODE')):
                if(data[i]['CORROSION_ENVIRONMENT_NODE']){
                    loopNodeProperty(data[i]['CORROSION_ENVIRONMENT_NODE'], CORROSION_ENVIRONMENT_NODE, 'CORROSION_ENVIRONMENT_NODE', '腐蚀环境调查', i, len, id);
                }
                break;
            case (data[i].hasOwnProperty('CTRL_LEAKAGE_POINT_NODE')):
                if(data[i]['CTRL_LEAKAGE_POINT_NODE']){
                    loopNodeProperty(data[i]['CTRL_LEAKAGE_POINT_NODE'], CTRL_LEAKAGE_POINT_NODE, 'CTRL_LEAKAGE_POINT_NODE', '漏损点测量', i, len, id);
                }
                break;
            case (data[i].hasOwnProperty('COAT_SURVEY_NODE')):
                if(data[i]['COAT_SURVEY_NODE']){
                    loopNodeProperty(data[i]['COAT_SURVEY_NODE'], COAT_SURVEY_NODE, 'COAT_SURVEY_NODE', '防腐层调查', i, len, id);
                }
                break;
            case (data[i].hasOwnProperty('SURFACE_TREATMENT_NODE')):
                if(data[i]['SURFACE_TREATMENT_NODE']){
                    loopNodeProperty(data[i]['SURFACE_TREATMENT_NODE'], SURFACE_TREATMENT_NODE, 'SURFACE_TREATMENT_NODE', '表面处理', i, len, id);
                }
                break;
            case (data[i].hasOwnProperty('CP_SURVEY_NODE')):
                if(data[i]['CP_SURVEY_NODE']){
                    loopNodeProperty(data[i]['CP_SURVEY_NODE'], CP_SURVEY_NODE, 'CP_SURVEY_NODE', '阴保调查', i, len, id);
                }
                break;
            case (data[i].hasOwnProperty('COAT_REPAIR_NODE')):
                if(data[i]['COAT_REPAIR_NODE']){
                    loopNodeProperty(data[i]['COAT_REPAIR_NODE'], COAT_REPAIR_NODE, 'COAT_REPAIR_NODE', '防腐层修复', i, len, id);
                }
                break;
            case (data[i].hasOwnProperty('END_NODE')):
                if(data[i]['END_NODE']){
                    loopNodeProperty(data[i]['END_NODE'], END_NODE, 'END_NODE', '管沟回填', i, len, id);
                }
                break;
            default:
                break;
        }
    }
    /** 主体 - end **/

    $repair_status.html(recordContent.head);
    $repair_body.html(recordContent.body);
    $dy_box.append($repair_status);
    $dy_box.append($repair_body);

    // 追加完成标识和按钮
    $repair_buttons.append($pick_btn);
    if (status == 'ALREADY_REPAIR' || status == 'DIFFICULT_TO_REPAIR') {
        $dy_box.append($all_done);
        $repair_buttons.append($work_btn);
    }
    $repair_buttons.append('<div class="clear"></div>');

    // 追加到tr
    $dy_box.append($repair_buttons);
    tr.after($dy_tr);
}

function loopNode(data, id, number, tr, status, tag) {
    if (data) {
        if ($.isArray(data) && data.length > 0) {
            loopNodeData(data, id, number, tr, status, tag);
        } else {
            var $empty_tr = tr.siblings('tr[data-empty="' + id + '"]');
            if($empty_tr.length > 0){
                $empty_tr.show();
            }else{
                tr.after('<tr class="zy-dy-tr zy-dy-empty" data-empty="' + id + '"><td colspan="11"><div class="zy-datatable-empty"><p><img src="' + DyyBase.root_path + '/static/img/chart_no_data.png"></p><div class="zy-empty-footer"><button type="button" class="zy-button zy-pick-up zy-pick-empty"><i></i>收起</button></div></div></td></tr>');
            }
        }
    } else {
        $(this).MSG({
            type: 'error',
            content: '缺少必要的记录数据!'
        });
        return false;
    }
}



